#!/bin/sh 

PTYPES="boolean Boolean false none #\
        byte Byte (byte)0 none #\
        char Character (char)0 none #\
        short Short (short)0 none #\
        int Integer 0 none #\
        float Float 0.0f none #\
        long Long 0L none #\
        double Double 0.0 none "

echo "${PTYPES}" | tr '#' '\n' | while read xxx XXX DDD III; do
    idstr='Id'
    if [ "$xxx" = "boolean" ]; then
        deftest="!this.value";
    else
        deftest="this.value == ${DDD}";
    fi
    cat > XML"${XXX}"Field.java << xxEOFxx

/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * \$${idstr}\$
 */

package org.dellroad.sidekar.xml;

xxEOFxx

if [ "${III}" != "none" ]; then
    printf 'import %s;\n\n' "${III}" >> XML"${XXX}"Field.java
fi

    cat >> XML"${XXX}"Field.java << xxEOFxx
/**
 * Container for a {@link ${XXX}Field}'s value during the Sidekar XML import/export process.
 */
class XML${XXX}Field extends XMLAtomicField<${XXX}> {

    private ${xxx} value;

    // Used during unmarshalling
    private XML${XXX}Field(XMLItem xmlItem) {
        super(xmlItem);
    }

    // Used during unmarshalling
    private XML${XXX}Field(XMLField xmlField) {
        super(xmlField);
    }

    // Used during marshalling
    public XML${XXX}Field(String name, ${xxx} value) {
        super(name);
        this.value = value;
    }

    // Used during unmarshalling
    @Override
    public ${XXX} getObjectValue() {
        return this.value;
    }

xxEOFxx

    if echo "${xxx}" | grep -qv '[^a-z]'; then
    cat >> XML"${XXX}"Field.java << xxEOFxx
    // Used during marshalling
    @Override
    public boolean hasDefaultValue() {
        return ${deftest};
    }

xxEOFxx
    fi

    cat >> XML"${XXX}"Field.java << xxEOFxx
    // Used during unmarshalling
    public static XML${XXX}Field newInstance(Object obj) {
        return obj instanceof XMLItem ? new XML${XXX}Field((XMLItem)obj) : new XML${XXX}Field((XMLField)obj);
    }
}

xxEOFxx

done
